import { EggAppConfig, EggAppInfo, PowerPartial } from 'egg';
// import { IApplicationLocals } from '../../typings/app';

export type DefaultConfig = PowerPartial<EggAppConfig>;

export default (appInfo: EggAppInfo) => {
  const config = {} as DefaultConfig;

  // use for cookie sign key, should change to your own and keep security
  config.keys = appInfo.name + '_1619420999976_6928';

  // add your config here
  config.middleware = ['errorMiddleware', 'jwtTokenHandle']; // 全局使用

  config.midwayFeature = {
    // true 代表使用 midway logger
    // false 或者为空代表使用 egg-logger
    replaceEggLogger: true,
  };

  // 关闭安全校验(开启跨域访问)
  config.security = {
    csrf: {
      enable: false,
      ignoreJSON: true
    },
    domainWhiteList: [ '*' ],
  }
  config.cors = {
    // enable:  true,
    // package: 'egg-cors',
    // origin:'http://127.0.0.1:3000', //这种方式是允许所有的IP+端口跨域访问
    credentials: true, // 开启认证
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH,OPTIONS', //允许请求的方式
  }

  // token校验机制(ignore中部分路径忽略token校验)
  config.jwt = {
    secret: 'backend',
    expiresIn: 60*60*2 // 2小时过期
  }

  config.jwtTokenHandle = {
    allowed: [
      '/user/login',
      '/user/code',
      '/public'
    ]
  }

  config.orm = {
    type: 'mysql',
    host: '127.0.0.1',
    port: 3306,
    username: 'root',
    password: '123456',
    database: 'backend',
    synchronize: true,
    logging: false,
  }

  return config;
};

